
var app = getApp()
Page({
  data: {
    ossImg: `${app.globalData.imgUrl}/applet_web_images/new_image`,
    imgUrl: getApp().globalData.imgUrl,
    appInfo: getApp(),
    relet_order_info: {}, // 续租信息
    reletImg: '', //续租图片
    customSertviceHide: false, // 客服弹窗
    relet_order_no: '', // 续租订单号
    moreHidden: false, // 更多按钮
    parent_order_id: '', // 续租的订单号
    parent_order_type: '', //续租的订单类型
    relet_scheme_id: '', //续租方案id
    is_show_continue_relet_btn: 0, // 是否显示继续续租
    is_show_relet_fail_explain: 0, // 是否显示续租失败文案
    is_show_relet_bill_btn: 0 // 是否显示还款账单
  },
 
  onLoad(options) {
    let {
      relet_order_no, parent_order_id, parent_order_type, relet_scheme_id,platform
    } = options
    this.setData({
      relet_order_no,
      parent_order_id, 
      parent_order_type, 
      relet_scheme_id
    })
    let app = getApp()
    if(platform) {
      app.globalData.platform = platform
    }
  },

  onReady() {
    this.initOrderDetail()
  },

  // 继续续租
  continueRelet() {
    const {
      parent_order_id, 
      parent_order_type, 
      relet_scheme_id
    } = this.data
    let app = getApp()
    my.showLoading({
      content: '加载中...',
      delay: 0,
    });
    my.request({
      url: `${app.globalData.testUrl}/api/relet/continueRelet`,
      method: 'POST',
      data: {
        parent_order_id, 
        parent_order_type, 
        relet_scheme_id,
        zm_user_id: app.globalData.user_id, // app.globalData.user_id
      },
      success: (res) => {
        const { 
          data: { 
            data, 
            code, msg: content } 
        } = res
        if(code !== 1) {
          my.showToast({
            type: 'none',
            content,
            duration: 1000
          })
          my.hideLoading();
          return
        }
        const { orderStr, relet_order_no, is_freeze_deposit } = data
        if(!is_freeze_deposit) {
          my.navigateTo({
            url: `/page/mine/myorders/renewalSuccess/renewalSuccess?relet_order_no=${relet_order_no}`
          })
          return
        }
        my.tradePay({
          orderStr,
          success: (res) => {
            const { resultCode: code  } = res
            if(code === '9000') {
              my.showToast({
                type: 'none',
                content: '续租成功',
                duration: 1000,
                success: () => {
                  my.navigateTo({
                    url: `/page/mine/myorders/renewalSuccess/renewalSuccess?relet_order_no=${relet_order_no}`
                  })
                }
              })
            } else {
              my.showToast({
                type: 'none',
                content: '续租未完成',
                duration: 1000,
              });
            }
          },
          fail: () => {
            my.showToast({
              type: 'none',
              content: '续租未完成',
              duration: 1000,
            });
          },
          complete: () => {
            my.hideLoading();
          }
        })
      }
    })
  },

  initOrderDetail() {
    let app = getApp()
    const { relet_order_no } = this.data
    my.request({
      url: `${app.globalData.testUrl}/api/relet/getReletOrderDetail`,
      method: 'POST',
      data: {
        relet_order_no,
        zm_user_id: app.globalData.user_id
      },
      success: ({data: {data: relet_order_info, code, msg: content}}) => {
        if(code !== 1) {
            my.showToast({
              type: 'none',
              content,
              duration: 1000
            })
            return
          }

          const selectStateImg = {
            0: '/image/relet/relet_error.png',
            4: '/image/relet/renewal_in_progress.png',
            6: '/image/relet/overdue.png',
            10: '/image/relet/complete.png',
          }

          let {
            is_show_continue_relet_btn,
            is_show_relet_fail_explain,
            is_show_relet_bill_btn,
            order_status
          } = relet_order_info
          this.setData({
            reletImg: selectStateImg[order_status],
            relet_order_info,
            is_show_continue_relet_btn,
            is_show_relet_fail_explain,
            is_show_relet_bill_btn
          })
      }
    })
  },
  // 显示客服
  showCustomSertvice() {
    this.setData({
      customSertviceHide: true
    })
  },
  // 还款
  repayment() {
    const { relet_order_no } = this.data
    my.navigateTo({
      url: `/page/mine/myorders/renewalBill/renewalBill?relet_order_no=${relet_order_no}`
    })
  },
  // 关闭客服
  closeCustomSertvice() {
    this.setData({
      customSertviceHide: false
    })
  },
  // 打电话
  callPhone() {
    let app = getApp();
    my.makePhoneCall({
      number: app.globalData.service_phone,
    });
  },
  jumpPage({target: {dataset:{url}}}) {
    my.navigateTo({
      url
    })
  },
  jumpHomePage() {
    my.switchTab({
      url: '/page/rent/rent'
    })
  },
  // 打开更多
  openMore(moreHidden) {
    this.setData({
      moreHidden
    })
  }
});
